<?php

class EventsController extends AppController {

/**
 * Uses array
 *
 * @var array
 */
	public $name = 'Events';

  //public $components = array('Auth');
  public $helpers = array('Html', 'Form', 'Paginator', 'Js' => array('Jquery'));

/**
 * Pagination
 *
 * @var array
 **/
  public $paginate = array(
        'limit' => 15
  );
  public function beforeFilter() {
      parent::beforeFilter();
  }

  /**
 * @method: index
 * @purpose: list of all events
 * @param none
 * @return void
 **/
  function admin_index() {
		$this->Event->recursive = 1;
		$this->set('events', $this->paginate());
	}

/**
 * @method: view
 * @purpose: view events
 * @param none
 * @return void
 **/
	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('event', $this->Event->read(null, $id));
	}

/**
 * @method: add
 * @purpose: admin add event
 * @param none
 * @return void
 **/
	function admin_add() {
		if (!empty($this->data)) {
			$this->Event->create();
			if ($this->Event->save($this->data)) {
				$this->Session->setFlash(__('The event has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
			}
		}
		$this->set('eventTypes', $this->Event->EventType->find('list'));
	}

/**
 * @method: edit
 * @purpose: for admin to update event
 * @param none
 * @return void
 **/
	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Event->save($this->data)) {
				$this->Session->setFlash(__('The event has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Event->read(null, $id);
		}
		$this->set('eventTypes', $this->Event->EventType->find('list'));
	}

/**
 * @method: delete
 * @purpose: for admin to delete event
 * @param none
 * @return void
 **/
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for event', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Event->delete($id)) {
			$this->Session->setFlash(__('Event deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Event was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

        // The feed action is called from "webroot/js/ready.js" to get the list of events (JSON)
	function admin_feed($id=null) {
		$this->layout = "ajax";
		$vars = $this->params['url'];
    $absolute_path = Router::url('/', true);
/***  load all events within dates  **/

		$conditions = array('conditions' => array('UNIX_TIMESTAMP(start) >=' => $vars['start'], 'UNIX_TIMESTAMP(start) <=' => $vars['end']));
		$events = $this->Event->find('all', $conditions);
		$data = array();
    foreach($events as $event) {
			if($event['Event']['all_day'] == 1) {
				$allday = true;
				$end = $event['Event']['start'];
			} else {
				$allday = false;
				$end = $event['Event']['end'];
			}
			$data[] = array(
					'id' => $event['Event']['id'],
					'title'=>$event['Event']['title'],
					'start'=>$event['Event']['start'],
					'end' => $end,
					'allDay' => $allday,
					'url' => $absolute_path.'admin/events/view/'.$event['Event']['id'],
					'details' => $event['Event']['details'],
					'className' => $event['EventType']['color']
			);
		}
    
/***  load all birthdays within dates  **/
    $this->loadModel('Customer');
    App::uses('CakeTime', 'Utility');

    $start = CakeTime::format('m-d', $vars['start']);
    $end = CakeTime::format('m-d', $vars['end']);

		$conditions = array('conditions' => array('DATE_FORMAT(Customer.birthday,"%m %d") >=' => $start, 'DATE_FORMAT(Customer.birthday,"%m %d") <=' => $end));
		$birthdays = $this->Customer->find('all', $conditions);
    //pr($birthdays); die('>>>.');
    foreach($birthdays as $birthday) {
			$allday = true;
      $date = date('Y').'-'.CakeTime::format('m-d', $birthday['Customer']['birthday']);
		 //die('>>>>>>>>');	
      $data[] = array(
					'id' => $birthday['Customer']['id'],
					'title'=> 'Happy Birthday',
					'start'=> $date,
					'end' => $date,
					'allDay' => $allday,
					'url' => $absolute_path.'admin/customers/view/'.$birthday['Customer']['id'],
					'details' => $birthday['Customer']['f_name'].' '.$birthday['Customer']['l_name'],
					'className' => 'Red'
			);
		}
    
		$this->set("json", json_encode($data));
	}

/**
 * @method: update
 * @purpose: The update action is called from "webroot/js/ready.js" to update date/time when an event is dragged or resized 
 * @param none
 * @return void
 **/
	function admin_update() {
    $this->autoRender = false;
		$vars = $this->params['url'];
		$this->Event->id = $vars['id'];
		$this->Event->saveField('start', $vars['start']);
		$this->Event->saveField('end', $vars['end']);
		$this->Event->saveField('all_day', $vars['allday']);
	}

/**
 * @method: index
 * @purpose: list of all events for customer
 * @param none
 * @return void
 **/
  function index() {
		$this->Event->recursive = 1;
		$this->set('events', $this->paginate());
    /*
    $this->loadModel('Customer');
    App::uses('CakeTime', 'Utility');

    $today = CakeTime::format('m-d');
		$conditions = array('conditions' => array('DATE_FORMAT(Customer.birthday,"%m %d") >=' => $today));
		$birthdays = $this->Customer->find('all', $conditions);
    $this->set('birthdays', $birthdays);
    */
	}

/**
 * @method: view
 * @purpose: view event to customer
 * @param none
 * @return void
 **/
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('event', $this->Event->read(null, $id));
	}


}